<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/


define('CONFIG_ROOT', '/etc/bh/');
define('CODE_ROOT', '/opt/hosting/daemons/includes/');
define('NAMED', '/etc/bind/named.conf');
include CODE_ROOT."common.php";
//////////////////////////////////////////////////////
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "From: Breizh Héberg Bot <robot@breizh-heberg.net>\n";
$headers .= "Reply-To: Support Breizh-Héberg <support@breizh-heberg.net>\n";
//////////////////////////////////////////////////////
@unlink(NAMED);
$_named = fopen(NAMED, "a+");
fputs($_named,file_get_contents(CONFIG_ROOT."tpl/bind-secondaire-modele.html"));
fputs($_named,'zone "breizh-web.net" { type slave; file "/etc/bind/db.breizh-web.net"; masters{'.$server[0]['ip'].';}; };'."\n");
fputs($_named,'zone "breizh-heberg.net" { type slave; file "/etc/bind/db.breizh-heberg.net"; masters{'.$server[0]['ip'].';}; };'."\n");
fputs($_named,'zone "breizh-heberg.com" { type slave; file "/etc/bind/db.breizh-heberg.com"; masters{'.$server[0]['ip'].';}; };'."\n");
fputs($_named,'zone "breizh-heberg.eu" { type slave; file "/etc/bind/db.breizh-heberg.eu"; masters{'.$server[0]['ip'].';}; };'."\n");
fputs($_named,'zone "breizh-image.fr" { type slave; file "/etc/bind/db.breizh-image.fr"; masters{'.$server[0]['ip'].';}; };'."\n");
fputs($_named,'zone "canal-pc.fr" { type slave; file "/etc/bind/db.canal-pc.fr"; masters{'.$server[0]['ip'].';}; };'."\n");
fputs($_named,'zone "b-games.net" { type slave; file "/etc/bind/db.b-games.net"; masters{'.$server[0]['ip'].';}; };'."\n");
foreach($alias_bh as $_alias_bh) {
	fputs($_named,'zone "'.$_alias_bh.'" { type slave; file "/etc/bind/db.'.$_alias_bh.'"; masters{'.$server[0]['ip'].';}; };'."\n");
}

// Domaines Clients externes
$_liste_doms_h = query("SELECT
							hebergdata_doms.id AS d_id,
							hebergdata_doms.id_dom_bh,
							hebergdata_doms.domaine,
							hebergdata_doms.etat,
							hebergdata_doms.id_alias_bh,
							hebergdata_doms.id_heberg,
							comptes_heberg.id,
							comptes_heberg.serveur_apache
							FROM hebergdata_doms 
							LEFT JOIN comptes_heberg
							ON hebergdata_doms.id_heberg = comptes_heberg.id
							WHERE hebergdata_doms.id_alias_bh = 0 
							AND hebergdata_doms.id_dom_bh = 0");
While($liste_doms_h = mysql_fetch_array($_liste_doms_h)) {
	if($liste_doms_h['etat'] == 3 OR $liste_doms_h['etat'] == 6 OR $liste_doms_h['etat'] == 1) {
		fputs($_named,"\n".'zone "'.$liste_doms_h['domaine'].'" { type slave; file "/etc/bind/db.'.$liste_doms_h['domaine'].'"; masters{'.$server[0]['ip'].';}; };'."\n");
	}
}

//Domaines achetés sur BH
$_liste_doms_h = query("SELECT
							zones_dns.id as z_id,
							zones_dns.id_domain,
							zones_dns.etat,
							comptes_domain.id,
							comptes_domain.domaine
							FROM zones_dns
							LEFT JOIN comptes_domain
							ON zones_dns.id_domain = comptes_domain.id
							");
While($liste_doms_h = mysql_fetch_array($_liste_doms_h)) {
	if(($liste_doms_h['etat'] == 3 OR $liste_doms_h['etat'] == 1)AND($liste_doms_h['domaine'] != "")) {
		fputs($_named,"\n".'zone "'.$liste_doms_h['domaine'].'" { type slave; file "/etc/bind/db.'.$liste_doms_h['domaine'].'"; masters{'.$server[0]['ip'].';}; };'."\n");
	}
}

shell_exec("sudo /etc/init.d/bind9 restart");
deconnexionbdd();
?>
